﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PersonalAlbum.Data.Objects;

namespace PersonalAlbum.Data.Entities
{
    public class ConfigRepository : IRepository<Config>
    {
        private string _connectionString;
        public ConfigRepository(string connectionString)
        {
            this._connectionString = connectionString;
        }
        public List<Config> GetItems()
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return ImagesDB.ConfigSet.Select(c =>
                    new Config()
                    {
                        Description = c.Description,
                        Name = c.Name,
                        Value = c.Value,
                        Id = c.Id

                    }).ToList();
            }
        }
        public void AddItem(Config config)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.Config c = new ImagesDal.Config()
                {
                    Description = config.Description,
                    Value = config.Value,
                    Name = config.Name
                };
                ImagesDB.AddToConfigSet(c);
                ImagesDB.SaveChanges();
                config.Id = c.Id;
            }
        }

        public void UpdateItem(Config config)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.Config old = ImagesDB.ConfigSet.FirstOrDefault(c => c.Id == config.Id);
                if (old != null)
                {
                    old.Name = config.Name;
                    old.Value = config.Value;
                    old.Description = config.Description;
                    ImagesDB.SaveChanges();
                }
            }

        }


        public void DeleteItem(Config config)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                ImagesDal.Config old = ImagesDB.ConfigSet.FirstOrDefault(c => c.Id == config.Id);
                if (old != null)
                {
                    ImagesDB.DeleteObject(old);
                    ImagesDB.SaveChanges();
                }
            }
        }


        #region IRepository<Config> Members


        public PersonalAlbum.Data.Objects.Config GetItemById(int id)
        {
            using (var ImagesDB = new ImagesDal.SqlEntitiesProvider(this._connectionString))
            {
                return ImagesDB.ConfigSet.Where(c => c.Id == id).Select(c =>
                    new PersonalAlbum.Data.Objects.Config()
                    {
                        Description = c.Description,
                        Name = c.Name,
                        Value = c.Value,
                        Id = c.Id

                    }).SingleOrDefault();
            }
        }

        #endregion
    }
}
